﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace DynamicGeometry
{
    public class BehaviorOrderer
    {
        public static void Order(List<Behavior> behaviors)
        {
            behaviors.Sort((x, y) => GetOrder(x).CompareTo(GetOrder(y)));
        }

        public static string GetCategory(Behavior behavior)
        {
            if (behavior is UserDefinedTool)
            {
                return "Custom";
            }
            var type = behavior.GetType();
            var result = "Misc";
            var foundCategory = orders.Where(kvp => kvp.Value.Contains(type)).FirstOrDefault();
            if (!foundCategory.Key.IsEmpty())
            {
                result = foundCategory.Key;
            }
            return result;
        }

        private static Dictionary<string, Type[]> orders = new Dictionary<string, Type[]>()
        {
            {"Points", new [] {
                typeof(Dragger),
                typeof(FreePointCreator),
                typeof(MidpointCreator),
                typeof(PolygonCreator),
                typeof(DistanceMeasurementCreator),
                typeof(AreaMeasurementCreator),
            }}, {"Lines", new [] {
                typeof(SegmentCreator),
                typeof(RayCreator),
                typeof(LineTwoPointsCreator),
                typeof(ParallelLineCreator),
                typeof(PerpendicularLineCreator),
                typeof(SegmentBisectorCreator),
                typeof(AngleBisectorCreator),
                typeof(DivideSegmentCreator),
                typeof(JoinTwoSegmentsCreator),
            }}, {"Circles", new [] {
                typeof(CircleCreator),
                typeof(CircleByRadiusCreator),
                typeof(ArcCreator),
            }}, {"Shapes", new [] {
                typeof(TriangleCreator),
                typeof(SquareCreator),
                
            }}, {"Coordinates", new [] {
                typeof(FunctionGraphCreator),
                typeof(LineByEquationCreator),
                typeof(CircleByEquationCreator),
                typeof(CoordinatesMeasurementCreator),
            }}, {"Transformations", new [] {
               typeof(ReflectionCreator),
               typeof(RotationCreator),
               typeof(TranslationCreator),
               typeof(DilationCreator),
            }}, {"Measure", new [] {
                
                typeof(AngleMeasurementCreator),
                
            }}, {"Misc", new [] {
                typeof(BezierCreator),
                typeof(LocusCreator),
                typeof(LabelCreator),
            }}, {"Custom", new [] {
                typeof(MacroDefiner)
            }},
        };

        static Type[] linearOrders = orders.SelectMany((kvp, index) => kvp.Value).ToArray();

        public static int GetOrder(Behavior behavior)
        {
            return GetOrder(behavior.GetType());
        }

        static int GetOrder(Type behaviorType)
        {
            for (int i = 0; i < linearOrders.Length; i++)
            {
                if (linearOrders[i] == behaviorType)
                {
                    return i;
                }
            }
            return 1000;
        }
    }
}
